package com.vue.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vue.entity.es.EsUserDto;
import com.vue.entity.es.EsUserEntity;
import com.vue.mapper.EsIdxPersonDao;
import com.vue.mapper.EsIdxPersonMapper;
import com.vue.service.EsIdxPersonService;
import com.vue.util.EsUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * @author: wrj
 * @since: 2025/1/6 16:12
 * @description:
 */

@Slf4j
@Service
public class EsIdxPersonServiceImpl implements EsIdxPersonService {

    private final static String indexName = "idx_user";

    @Resource
    private EsIdxPersonDao idxPersonDao;
    @Resource
    private EsIdxPersonMapper idxPersonMapper;
    @Autowired
    private RestHighLevelClient client;

    /**
     * 目前支持 汉字、拼音全拼、拼音缩写、汉字拼音混合搜索人员姓名。
     * 如果后期需要增加其他的查询条件，在此方法构建即可。
     * 如果有其他的字段有需求需要更改索引的 Mapping。详细可以参考一下 doc目录下的 ES人员信息索引维护.docx
     * @param dto
     * @return
     * @throws IOException
     */
    @Override
    public List<EsUserEntity> searchUser(EsUserDto dto) throws IOException {
        // 查询条件
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
//        if (!StringUtils.isBlank(dto.getRealName())) {
//            boolQueryBuilder.must(QueryBuilders.matchPhraseQuery("real_name", dto.getRealName()));
//        }
        if (!StringUtils.isBlank(dto.getPhone())) {
            boolQueryBuilder.must(QueryBuilders.termQuery("phone",dto.getPhone()));
        }
        if (dto.getSex() != null) {
            boolQueryBuilder.must(QueryBuilders.termQuery("sex", dto.getSex()));
        }
        // 将用户输入的姓名中的汉字拆分成一个个的汉字
        String[] chineseList = StringUtils.split(dto.getRealName(), "", -1);
        if (CollectionUtils.isNotEmpty(Arrays.asList(chineseList))) {
            for (String chinese : chineseList) {
                boolQueryBuilder.filter(QueryBuilders.termQuery("real_name", chinese));
            }
        }

        return EsUtil.search(indexName,dto.getPageNum(),dto.getPageSize(),client,boolQueryBuilder,EsUserEntity.class);
    }

    /**
     * 将人员信息维护进 ES文档中。此方法需要手动执行。
     */
    public void savePersonData(Integer pageNum, Integer pageSize){
        IPage page = new Page(pageNum, pageSize);
//        idxPersonDao.deleteAll();
        // 查询人员数据
        List<EsUserEntity> esIdxPersonEneities = idxPersonMapper.queryAllPerson(page);
//        idxPersonDao.saveAll(esIdxPersonEneities);
    }

    @Override
    public void saveOnePersonData(EsUserEntity esUserEntity) {
        log.info("保存es数据：{}", JSON.toJSON(esUserEntity));
//        idxPersonDao.save(esUserEntity);
    }

}

